package com.niuwa.p2p.action.fundmanager;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.entity.fund.ApplyAcctLog;
import com.niuwa.p2p.entity.fund.BaseAccount;
import com.niuwa.p2p.entity.fund.ChannelAccount;
import com.niuwa.p2p.entity.fund.ChlAcctRule;
import com.niuwa.p2p.entity.fund.CustAccount;
import com.niuwa.p2p.entity.fund.CustAcctRule;
import com.niuwa.p2p.entity.fund.PayPartner;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.service.fund.FundAccountService;
import com.niuwa.p2p.service.fund.IPayPartnerService;
/**
 * 处理资金用户账户和渠道账户以及渠道账户审核
 * @author money
 *
 */
@Namespace("/fundmanager")
@Controller
public class FundAction extends JsonAction{
	Logger log = Logger.getLogger(FundAction.class);

	private static final long serialVersionUID = 1L;
//	@Autowired
	private FundAccountService fundAccountService;
	
	@Autowired
	private IPayPartnerService payPartnerService;
	
	private BaseAccount baseAccount;
	private ChannelAccount channelAccount;
	private ChlAcctRule chlAcctRule;
	private CustAcctRule custAcctRule;
	
	
	
	public ChlAcctRule getChlAcctRule() {
		return chlAcctRule;
	}
	public void setChlAcctRule(ChlAcctRule chlAcctRule) {
		this.chlAcctRule = chlAcctRule;
	}
	public ChannelAccount getChannelAccount() {
		return channelAccount;
	}
	public void setChannelAccount(ChannelAccount channelAccount) {
		this.channelAccount = channelAccount;
	}
	public FundAccountService getFundAccountService() {
		return fundAccountService;
	}
	public void setFundAccountService(FundAccountService fundAccountService) {
		this.fundAccountService = fundAccountService;
	}
	public IPayPartnerService getPayPartnerService() {
		return payPartnerService;
	}
	public void setPayPartnerService(IPayPartnerService payPartnerService) {
		this.payPartnerService = payPartnerService;
	}
	public BaseAccount getBaseAccount() {
		return baseAccount;
	}
	public void setBaseAccount(BaseAccount baseAccount) {
		this.baseAccount = baseAccount;
	}
	
	public CustAcctRule getCustAcctRule() {
		return custAcctRule;
	}
	public void setCustAcctRule(CustAcctRule custAcctRule) {
		this.custAcctRule = custAcctRule;
	}
	/**
	 * 跳转客户账号列表
	 * @return
	 */
	@Action("gotoCustomerList")
	public String gotoCustomerList(){
		return "customerList";
	}
	/**
	 * 跳转渠道账号列表
	 * @return
	 */
	@Action("gotoCompanyList")
	public String gotoCompanyList(){
		return "companyList";
	}
	@Action("gotoCompanyAccountAduitList")
	public String gotoCompanyAccountAduitList(){
		return "companyAccountAuditList";
	}
	/**
	 * 查看客户账号详情
	 */
	@Action("gotoViewCustomerDetail")
	public String gotoCustomerDetail(){
		String acctNo = getParam("acctNo");
		String acctName = getParam("acctName");
		String remark = getParam("remark");
		String openOrgName = getParam("openOrgName");
		String acctBal = getParam("acctBal");
		String openTime = getParam("openTime");
		CustAccount custAccount = fundAccountService.getCustAccoutByKey(acctNo);
		
		this.getRequest().setAttribute("acctNo", acctNo);
		this.getRequest().setAttribute("acctName", acctName);
		this.getRequest().setAttribute("remark", remark);
		this.getRequest().setAttribute("openOrgName", openOrgName);
		this.getRequest().setAttribute("openTime", openTime);
		this.getRequest().setAttribute("acctBal", acctBal);
		this.getRequest().setAttribute("custAccount", custAccount);
		
		if(custAccount != null){
			//查询规则
			CustAcctRule custAcctRule = fundAccountService.getCustAcctRule(custAccount.getCustRuleId());
			this.getRequest().setAttribute("custAcctRule", custAcctRule);
		}
		return "viewCustomerDetail";
	}
	@Action("gotoSetCompanyDetail2")
	public String gotoSetCompanyDetail2(){
		String acctNo = this.getRequest().getParameter("acctNo");
		try {
			acctNo = URLDecoder.decode(acctNo.replaceAll("%", "%25"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		BaseAccount baseAccount = fundAccountService.getBaseAccountByAcctNo(acctNo);
		ChannelAccount channelAccount = fundAccountService.getChannelAccountByAcctNo(acctNo);
		String controlType = channelAccount.getControlType();
		String isDefault =  channelAccount.getIsDefault();
		ChlAcctRule chlAcctRule = null;
		CustAcctRule custAcctRule = null;
		if("STRONG".equals(controlType)){
			if("N".equals(isDefault)){
				chlAcctRule = fundAccountService.getChlAcctRuleById(channelAccount.getChlRuleId());
			}else if("Y".equals(isDefault)){
				chlAcctRule = fundAccountService.getChlRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
			}
		}else if("WEAK".equals(controlType)){
			if("N".equals(isDefault)){
				custAcctRule = fundAccountService.getCustAcctRule(channelAccount.getChlRuleId());
			}else if("Y".equals(isDefault)){
				custAcctRule = fundAccountService.getRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
			}
		}else{
			//在mc中绑定资金账户时，借款用户对于弱控制，渠道企业对应强控制
			log.info("数据库中数据错误，未设置强弱类型!");
		}
		if(chlAcctRule == null){
			chlAcctRule =  fundAccountService.getChlRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
		}
		if(custAcctRule == null){
			custAcctRule = fundAccountService.getRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
		}
		this.getRequest().setAttribute("baseAccount", baseAccount);
		this.getRequest().setAttribute("channelAccount", channelAccount);
		this.getRequest().setAttribute("chlAcctRule", chlAcctRule);
		this.getRequest().setAttribute("custAcctRule", custAcctRule);
		return "setCompanyDetail2";
	}
	/**
	 * 查看渠道用户详情
	 * @return
	 */
	@Action("gotoViewCompanyDetail")
	public String gotoCompanyDetail(){
	//	String acctNo = getParam("acctNo");
		String acctNo = this.getRequest().getParameter("acctNo");
		try {
			acctNo = URLDecoder.decode(acctNo.replaceAll("%", "%25"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		BaseAccount baseAccount = fundAccountService.getBaseAccountByAcctNo(acctNo);
		ChannelAccount channelAccount = fundAccountService.getChannelAccountByAcctNo(acctNo);
		String controlType = channelAccount.getControlType();
		String isDefault =  channelAccount.getIsDefault();
		ChlAcctRule chlAcctRule = new ChlAcctRule();
		CustAcctRule custAcctRule = new CustAcctRule();
		if("STRONG".equals(controlType)){
			if("N".equals(isDefault)){
				chlAcctRule = fundAccountService.getChlAcctRuleById(channelAccount.getChlRuleId());
			}else if("Y".equals(isDefault)){
				chlAcctRule = fundAccountService.getChlRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
			}
		}else if("WEAK".equals(controlType)){
			if("N".equals(isDefault)){
				custAcctRule = fundAccountService.getCustAcctRule(channelAccount.getChlRuleId());
			}else if("Y".equals(isDefault)){
				custAcctRule = fundAccountService.getRuleByOpenOrgNo(baseAccount.getOpenOrgCode());
			}
		}else{
			//在mc中绑定资金账户时，借款用户对于弱控制，渠道企业对应强控制
			log.info("数据库中数据错误，未设置强弱类型!");
		}
		this.getRequest().setAttribute("baseAccount", baseAccount);
		this.getRequest().setAttribute("channelAccount", channelAccount);
		this.getRequest().setAttribute("chlAcctRule", chlAcctRule);
		this.getRequest().setAttribute("custAcctRule", custAcctRule);
		/*return "viewCompanyDetail";*/
		return "viewCompanyDetail2";
	}
	/**
	 * 添加渠道账号
	 * @return
	 */
	@Action("gotoAddCompanyDetail")
	public String gotoAddCompanyDetail(){
		return "addCompanyDetail";
	}
	/**
	 * 设置客户账号详情
	 * @return
	 */
	@Action("gotoSetCustomerDetail")
	public String gotoSetCustomerDetail(){
		String acctNo = getParam("acctNo");
		String acctName = getParam("acctName");
		String remark = getParam("remark");
		String openOrgName = getParam("openOrgName");
		String acctBal = getParam("acctBal");
		String openTime = getParam("openTime");
		String openOrgCode = getParam("openOrgCode");
		CustAccount custAccount = fundAccountService.getCustAccoutByKey(acctNo);
		
		this.getRequest().setAttribute("acctNo", acctNo);
		this.getRequest().setAttribute("acctName", acctName);
		this.getRequest().setAttribute("remark", remark);
		this.getRequest().setAttribute("openOrgName", openOrgName);
		this.getRequest().setAttribute("openTime", openTime);
		this.getRequest().setAttribute("acctBal", acctBal);
		this.getRequest().setAttribute("openOrgCode", openOrgCode);
		this.getRequest().setAttribute("custAccount", custAccount);
		
		if(custAccount != null){
			//查询规则
			CustAcctRule custAcctRule = fundAccountService.getCustAcctRule(custAccount.getCustRuleId());
			this.getRequest().setAttribute("custAcctRule", custAcctRule);
		}
		return "setCustomerDetail";
	}
	/**
	 * 设置渠道用户详情
	 * @return
	 */
	@Action("gotoSetCompanyDetail")
	public String gotoSetCompanyDetail(){
		//String acctNo = getParam("acctNo");
		String acctNo = this.getRequest().getParameter("acctNo");
		try {
			acctNo = URLDecoder.decode(acctNo.replaceAll("%", "%25"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BaseAccount baseAccount = fundAccountService.getBaseAccountByAcctNo(acctNo);
		ChannelAccount channelAccount = fundAccountService.getChannelAccountByAcctNo(acctNo);
		ChlAcctRule chlAcctRule = fundAccountService.getChlAcctRuleById(channelAccount.getChlRuleId());
		this.getRequest().setAttribute("baseAccount", baseAccount);
		this.getRequest().setAttribute("channelAccount", channelAccount);
		this.getRequest().setAttribute("chlAcctRule", chlAcctRule);
		return "setCompanyDetail";
	}
	/**
	 * 获取开户机构
	 * @return
	 */
	@Action("getOpenOrg")
	public String getOpenOrg(){
		List<PayPartner> listPayParter = payPartnerService.selectAllValidPartnerName();
		this.setResult(true,"",listPayParter);
		return NONE;
	}
	/**
	 * 获取客户账号列表
	 * @return
	 */
	@Action("getCustomerList")
	public String getCustomerPageList(){
		setObjectReSult(fundAccountService.searchCustomerPage(getParamMap()));
		return NONE;
	}
	/**
	 * 获取渠道账号列表
	 * @return
	 */
	@Action("getChanelList")
	public String getChanelPageList(){
		setObjectReSult(fundAccountService.searchChanelPage(getParamMap()));
		return NONE;
	}
	/**
	 * 获取所有渠道账号
	 * @return
	 */
	@Action("getAllChanelData")
	public String getAllChannelData(){
		this.setResult(true,"",fundAccountService.getAllChannelAccount());
		return NONE;
	}
	/**
	 * 获取渠道账号审核列表
	 * @return
	 */
	@Action("getAcctApplyList")
	public String getAcctApplyList(){
		setObjectReSult(fundAccountService.searchAcctApplyPage(getParamMap()));
		return NONE;
	}
	
	/**
	 * 账户审核通过
	 * @return
	 */
	@Action("acctApplyPass")
	public String applyPass(){
		String applyNo = getParam("applyNo");
		Long userId = ((User)this.getRequest().getSession().getAttribute("user")).getId();
		boolean result = fundAccountService.saveAcctApplyPass(applyNo, userId);
		
		if(result){
			this.setResult(true, "操作成功!", null);
			System.out.println("========调用第三方接口=======");
		}else{
			this.setResult(false, "该条记录已经审核,请刷新!", null);
		}
			
		return NONE;
	}
	/**
	 * 账户申请拒绝通过
	 */
	@Action("acctApplyReject")
	public String applyReject(){
		String reason = getParam("reason");
		String applyNo = getParam("applyNo");
		Long userId = ((User)this.getRequest().getSession().getAttribute("user")).getId();
		boolean result = fundAccountService.saveAcctApplyReject(applyNo, userId, reason);
		if(result){
			this.setResult(true, "操作成功!", null);
		}else{
			this.setResult(false, "该条记录已经审核,请刷新!", null);
		}
		return 	NONE;
	}
	/**
	 * 最新账号审核结果
	 * @return
	 */
	@Action("applyAudit")
	public String applyAudit(){
		ApplyAcctLog applyAcctLog= fundAccountService.getNewestAudit(getParam("applyNo"));
		this.setResult(true, null, applyAcctLog);
		return NONE;
	}
	/**
	 * 根据机构编号获取客户账号机构规则
	 * @return
	 */
	@Action("getRuleByOpenOrgCode")
	public String getRuleByOpenOrgCode(){
		CustAcctRule custAcctRule = fundAccountService.getRuleByOpenOrgNo(getParam("openOrgCode"));
		this.setResult(true,null,custAcctRule);
		return NONE;
	}
	
	/**
	 * 根据机构编号获取渠道账号机构规则
	 * @return
	 */
	@Action("getChlRuleByOpenOrgCode")
	public String getChlRuleByOpenOrgCode(){
		ChlAcctRule chlAcctRule = fundAccountService.getChlRuleByOpenOrgNo(getParam("openOrgCode"));
		this.setResult(true,null,chlAcctRule);
		return NONE;
	}
	/**
	 * 添加渠道账号
	 * @return
	 */
	@Action("addChlAcct")
	public String addChlAcct(){
		//检查账号是否唯一
		boolean isHave = fundAccountService.isContainsAcctNo(baseAccount.getAcctNo());
		if(isHave){//账号已经存在
			this.setResult(false, "账号已经存在!", null);
			return NONE;
		}
		channelAccount.setAcctNo(baseAccount.getAcctNo());
		boolean result = fundAccountService.saveChannelAccount(baseAccount, channelAccount, chlAcctRule);
		if(result){
			this.setResult(true, "添加成功", null);
		}else{
			this.setResult(false, "添加失败", null);
		}
		return NONE;
	}
	/**
	 * 更新渠道账号
	 * @return
	 */
	@Action("updateChlAcct")
	public String updateChlAcct(){
		channelAccount.setAcctNo(baseAccount.getAcctNo());
		boolean result = fundAccountService.updateChannelAccount(baseAccount, channelAccount, chlAcctRule);
		if(result){
			this.setResult(true, "更新成功", null);
		}else{
			this.setResult(false, "更新失败", null);
		}
		return NONE;
	}
	@Action("updateChlAcct2")
	public String updateChlAcct2(){
		boolean result = fundAccountService.updateChannelAccount(baseAccount, channelAccount, chlAcctRule,custAcctRule);
		if(result){
			this.setResult(true, "更新成功", null);
		}else{
			this.setResult(false, "更新失败", null);
		}
		return NONE;
	}
}

